/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.xy.biz.rs.domain.constants.ManufactureArchiveConstants;
import com.xy.biz.rs.domain.converter.ManufactureArchiveRecordConverter;
import com.xy.biz.rs.domain.converter.ManufactureRecordConverter;
import com.xy.biz.rs.domain.req.*;
import com.xy.biz.rs.domain.resp.GetManufactureArchiveRecordResponse;
import com.xy.biz.rs.domain.resp.GetManufactureRecordResponse;
import com.xy.tool.context.UserContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.rs.domain.resp.GetManufactureArchiveResponse;
import com.xy.biz.rs.domain.query.ManufactureArchiveQuery;
import com.xy.biz.rs.domain.converter.ManufactureArchiveConverter;
import com.xy.biz.rs.client.ManufactureArchiveClient;
import com.xy.biz.rs.service.ManufactureArchiveService;

/**
 *
 *
 * @author wwj
 * @date 2021-4-19
 */
@Api(tags = "批生产记录")
@Slf4j
@RestController
@RequestMapping("/api/rs/manufacture/archive")
public class ManufactureArchiveController implements ManufactureArchiveClient {

	@Autowired
	private ManufactureArchiveService manufactureArchiveService;

	@ApiOperation(value = "保存批生产记录")
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveManufactureArchive(@RequestBody SaveManufactureArchiveRequest request) {
		return manufactureArchiveService.saveManufactureArchive(ManufactureArchiveConverter.toBO(request));
	}

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllManufactureArchive(@RequestBody Collection<SaveManufactureArchiveRequest> collection) {
		manufactureArchiveService.saveAllManufactureArchive(ManufactureArchiveConverter.toBO(collection));
		return Result.success();
	}

	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateManufactureArchive(@RequestBody SaveManufactureArchiveRequest request) {
		manufactureArchiveService.updateManufactureArchive(ManufactureArchiveConverter.toBO(request));
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteManufactureArchive(@RequestBody GetManufactureArchiveRequest request) {
		manufactureArchiveService.deleteManufactureArchive(request.getManufactureArchiveId());
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetManufactureArchiveResponse> getManufactureArchive(@RequestBody GetManufactureArchiveRequest request) {
		return Result.success(ManufactureArchiveConverter.toResponse(manufactureArchiveService.getManufactureArchive(request.getManufactureArchiveId())));
	}

	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetManufactureArchiveResponse>> listManufactureArchiveById(@RequestBody Collection<Long> collection) {
		return Result.success(ManufactureArchiveConverter.toResponse(manufactureArchiveService.listManufactureArchiveById(collection)));
	}

	@PostMapping(value = "/list")
	@Override
	public Result<List<GetManufactureArchiveResponse>> listManufactureArchive(@RequestBody ListManufactureArchiveRequest request) {
		return Result.success(ManufactureArchiveConverter.toResponse(manufactureArchiveService.listManufactureArchive(ManufactureArchiveConverter.toQuery(request))));
	}

	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countManufactureArchive(@RequestBody ListManufactureArchiveRequest request) {
		return Result.success(manufactureArchiveService.countManufactureArchive(ManufactureArchiveConverter.toQuery(request)));
	}

	@ApiOperation(value = "分页")
	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetManufactureArchiveResponse>> pageManufactureArchive(@RequestBody ListManufactureArchiveRequest request) {
		ManufactureArchiveQuery query = ManufactureArchiveConverter.toQuery(request);
		List<Integer> statusList = new ArrayList<>();
		statusList.add(ManufactureArchiveConstants.STATUS_UNAPPLY);
		statusList.add(ManufactureArchiveConstants.STATUS_APPROVED);
		query.setStatusList(statusList);
		query.setCreateUserId(UserContextHolder.getUserId());
		List<GetManufactureArchiveResponse> list = ManufactureArchiveConverter.toResponse(manufactureArchiveService.listManufactureArchive(query));
		int count = manufactureArchiveService.countManufactureArchive(query);
		return Result.success(new Page<>(list, count));
	}

    @ApiOperation(value = "查看")
    @PostMapping(value = "/detail")
    public Result<List<GetManufactureRecordResponse>> detail(@RequestBody GetManufactureArchiveRequest request) {
        return Result.success(ManufactureRecordConverter.toResponse(manufactureArchiveService.detail(request)));
    }

	@ApiOperation(value = "提交QA审核")
	@PostMapping(value = "/apply_check")
	public Result<?> applyCheck(@RequestBody ManufactureArchiveApplyCheckRequest request) {
		return manufactureArchiveService.applyCheck(request);
	}

	@ApiOperation(value = "QA审核分页")
	@PostMapping(value = "/check_page")
	public Result<?> ManufactureArchiveCheck(@RequestBody ListManufactureArchiveRequest request) {
		ManufactureArchiveQuery query = ManufactureArchiveConverter.toQuery(request);
		List<Integer> statusList = new ArrayList<>();
		statusList.add(ManufactureArchiveConstants.STATUS_APPLY);
		query.setStatusList(statusList);
		query.setCheckUserId(UserContextHolder.getUserId());
		List<GetManufactureArchiveResponse> list = ManufactureArchiveConverter.toResponse(manufactureArchiveService.listManufactureArchive(query));
		int count = manufactureArchiveService.countManufactureArchive(query);
		return Result.success(new Page<>(list, count));
	}

	@ApiOperation(value = "QA审核")
	@PostMapping(value = "/check")
	public Result<?> check(@RequestBody ManufactureArchiveQACheckRequest request) {
		return manufactureArchiveService.check(request);
	}

}
